﻿/* ==============================================================================
 * 功能描述：ACCWcfClient  
 * 创 建 者：ljbyo
 * 创建日期：2017/2/6 13:16:27
 * ==============================================================================*/

using ACC.Data.Entities.Common;
using ACC.MgrUtils.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace iPlant.Framework.ACC.WCF.WCF
{

    
    /// ACCWcfClient  
    /// </summary>
    public class ACCWcfClient
    {
        private ACCClient accClient = null;
        public ACCWcfClient(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                accClient = new ACCClient(url);
            }
        }

        public ClientResponse ExecuteCmd(ClientRequest cmd)
        {
            ClientResponse wcfResult = new ClientResponse();
            if (accClient != null)
            {
                Hashtable param = new Hashtable();
                XmlDocument xmlreq = IntfUtils.CreateRequestXml("client", "client", param);
                XmlElement rootelem = (XmlElement)xmlreq.SelectSingleNode("root");
                XmlElement xmldata = xmlreq.CreateElement("data");
                rootelem.AppendChild(xmldata);
                XmlCDataSection cdata = xmlreq.CreateCDataSection(JsonHelper.CompressString(JsonHelper.ToJson(cmd)));
                xmldata.AppendChild(cdata);

                string resultxml;
                XmlDocument xmldoc = new XmlDocument();

                try
                {
                    if (!accClient.SendXmlData("", xmlreq.OuterXml, out resultxml))
                    {
                        wcfResult.Result = false;
                        wcfResult.Message = "调用服务失败";
                    } 
                    xmldoc.LoadXml(resultxml);



                    //首先读取输入的XML 
                    XmlElement xmlprop = (XmlElement)xmldoc.SelectSingleNode("root/xmlprop");

                    string message = xmlprop.GetAttribute("message");
                    int result = 0;
                    int.TryParse(xmlprop.GetAttribute("result"), out result);
                    if (result != 1)
                    {
                        wcfResult.Result = false;
                        wcfResult.Message = "调用服务失败, " + message;
                    }
                    else
                    {
                        XmlNode dataNode = xmldoc.SelectSingleNode("root/data");
                        string data = dataNode == null ? "" : JsonHelper.DecompressString(dataNode.FirstChild.InnerText);
                        wcfResult = JsonHelper.ToObject<ClientResponse>(data);
                    }
                }
                catch (Exception ex)
                {
                    wcfResult.Result = false;
                    wcfResult.Message = "调用服务失败," + ex.Message;
                }
            }
            else
            {
                wcfResult.Result = false;
                wcfResult.Message = "初始化WCF服务失败";
            }
            return wcfResult;
        }
    }
}
